#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPainter>
#include <QMouseEvent>
#include <QDebug>
#include <QRect>
#include <QKeyEvent>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // 初始化速度
    speed = 15;
    // 更新红色区域的位置
    updateRedAreaPosition();
    createScoreBox();
    AttLabel->show();
    DefLabel->hide();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::updateRedAreaPosition() {
    // 计算小区域的新位置
    if(Rtop > 162){Rtop = 162;}
    redAreaRect = QRect(Rleft, Rtop, 80, 80);
    Rright = Rleft + 80;
    Rbottom = Rtop + 80;
    // 重绘窗口
    update();
}

void MainWindow::paintEvent(QPaintEvent *event){
   QPainter painter(this);
   painter.setRenderHint(QPainter::Antialiasing);
   // 绘制背景矩形
   int leftPadding = 30;
   painter.setPen(Qt::gray);
   painter.setBrush(Qt::blue);
   painter.drawRect(leftPadding, 0, 732, 244);

   // 绘制守门员区域
   painter.setPen(Qt::black);
   painter.setBrush(Qt::black);
   painter.drawRect(goalkeeperRect);

   painter.setPen(Qt::red);
   painter.setBrush(Qt::red);
   painter.drawEllipse(redAreaRect);

   painter.setPen(Qt::yellow);
   painter.setBrush(Qt::yellow);
   painter.drawEllipse(redOppRect);
}

void MainWindow::keyPressEvent(QKeyEvent *event)
{
    if(turn % 2 == 0){
    // 根据按下的方向键更新小区域的位置
    switch (event->key()) {
    case Qt::Key_Up:
        Rtop -= speed;
        Rbottom -= speed;
        updateRedAreaPosition();
        break;
    case Qt::Key_Down:
        Rtop += speed;
        Rbottom += speed;
        updateRedAreaPosition();
        break;
    case Qt::Key_Left:
        Rleft -= speed;
        Rright -= speed;
        updateRedAreaPosition();
        break;
    case Qt::Key_Right:
        Rleft += speed;
        Rright += speed;
        updateRedAreaPosition();
        break;
    case Qt::Key_Space: // 按下空格键生成随机区域
        AttLabel->hide();
        DefLabel->show();
        generateGoalkeeperPosition();
        double overlapArea = calculateOverlapArea(goalkeeperRect, redAreaRect);
        double redAreaSize = redAreaRect.width()*redAreaRect.width()/4*3.14;
        double overlapRatio = overlapArea / redAreaSize;
        if (overlapRatio >= 0.2) {

        }
        if(overlapRatio < 0.2){
            adm++;
        }
        updateScore();
        turn++;
        admtime--;
        victory();
        spt++;
        break;
    }
    // 重绘窗口
    update();
    }
    else{
        switch (event->key()) {
        case Qt::Key_Up:
            goalkeeperRect.adjust(0, -speed, 0, -speed);
            break;
        case Qt::Key_Down:
            goalkeeperRect.adjust(0, speed, 0, speed);
            break;
        case Qt::Key_Left:
            goalkeeperRect.adjust(-speed, 0, -speed, 0);
            break;
        case Qt::Key_Right:
            goalkeeperRect.adjust(speed, 0, speed, 0);
            break;
        case Qt::Key_Space: // 按下空格键生成随机区域
            generateRedArea();
            DefLabel->hide();
            AttLabel->show();
            double overlapArea = calculateOverlapArea(goalkeeperRect, redOppRect);
            double redAreaSize = redOppRect.width()*redOppRect.width()/4*3.14;
            double overlapRatio = overlapArea / redAreaSize;
            if (overlapRatio >= 0.2) {

            }
            if(overlapRatio < 0.2){
                opp++;
            }
            updateScore();
            turn++;
            opptime--;
            victory();
            spt++;
            break;
        }
        // 重绘窗口
        update();

    }
}

void MainWindow::generateGoalkeeperPosition() {
    // 定义范围
    int minX = 30; // 最小 x 坐标
    int maxX = 780; // 最大 x 坐标
    int minY = 0; // 最小 y 坐标
    int maxY = 244; // 最大 y 坐标
    // 生成随机位置
    int rectWidth = 300; // 守门员区域的宽度
    int rectHeight = 100; // 守门员区域的高度
    int x, y;
    do {
        x = qrand() % (maxX - minX) + minX; // 生成 x 坐标在指定范围内
        y = qrand() % (maxY - minY) + minY; // 生成 y 坐标在指定范围内
    } while (x + rectWidth > maxX || y + rectHeight > maxY); // 确保区域不超出范围
    goalkeeperRect = QRect(x, y, rectWidth, rectHeight);
}

void MainWindow::generateRedArea() {
    int minX = 30;
    int maxX = 780;
    int minY = 0;
    int maxY = 244;
    int rectSize = 80;
    int x, y;
    do {
        x = qrand() % (maxX - minX) + minX;
        y = qrand() % (maxY - minY) + minY;
    } while (x + rectSize > maxX || y + rectSize > maxY);
    redOppRect = QRect(x, y, rectSize, rectSize);
}

double MainWindow::calculateOverlapArea(const QRect &rect1, const QRect &rect2) {
    int x1 = std::max(rect1.left(), Rleft);
    int y1 = std::max(rect1.top(), Rtop);
    int x2 = std::min(rect1.right(), Rright);
    int y2 = std::min(rect1.bottom(), Rbottom);
    if (x1 < x2 && y1 < y2) {
        int width = x2 - x1;
        int height = y2 - y1;
        return width * height;
    } else {
        return 0;
    }
}

void MainWindow::createScoreBox() {
    QLabel *scoreLabel = new QLabel(this);
    scoreLabel->setText("Score");
    QLabel *admLabel = new QLabel(this);
    admLabel->setText("ADM");
    QLabel *oppLabel = new QLabel(this);
    oppLabel->setText("OPP");
    QLabel *turnLabel = new QLabel(this);
    turnLabel->setText("TURN");
    DefLabel = new QLabel(this);
    // 创建显示与adm相等比分的 QLabel
    admScoreLabel = new QLabel(this);
    admScoreLabel->setText(QString::number(adm)); // 将adm的值转换为QString显示
    // 设置样式
    admScoreLabel->setStyleSheet("font-size: 20px; color: blue;");

    // 创建显示与opp相等比分的 QLabel
    oppScoreLabel = new QLabel(this);
    AttLabel = new QLabel(this);
    oppScoreLabel->setText(QString::number(opp)); // 将opp的值转换为QString显示
    // 设置样式
    oppScoreLabel->setStyleSheet("font-size: 20px; color: red;");

    turnScoreLabel = new QLabel(this);
    turnScoreLabel->setText(QString::number(kic + 1));
    // 设置样式
    turnScoreLabel->setStyleSheet("font-size: 20px; color: green;");

    AttLabel->setFixedSize(800, 200);
    DefLabel->setFixedSize(800, 200);
    // 设置位置
    scoreLabel->move(390, 260); // 设置比分框的位置
    admLabel->move(280, 300); // 设置比分框的位置
    oppLabel->move(530, 300); // 设置比分框的位置
    turnLabel->move(300, 400); // 设置比分框的位置
    admScoreLabel->move(286, 330); // 设置adm比分的位置
    oppScoreLabel->move(536, 330); // 设置opp比分的位置
    turnScoreLabel->move(500, 400);

    AttLabel->setStyleSheet("font-size: 50px; color: purple;");
    AttLabel->setText("YOU ATTACK!");
    AttLabel->move(280, 400);

    DefLabel->setStyleSheet("font-size: 50px; color: orange;");
    DefLabel->setText("YOU DEFEND!");
    DefLabel->move(280, 400);
}

void MainWindow::updateScore() {
    admScoreLabel->setText(QString::number(adm)); // 更新admScoreLabel的文本内容
    oppScoreLabel->setText(QString::number(opp)); // 更新oppScoreLabel的文本内容
    turnScoreLabel->setText(QString::number(kic +1));
}

bool MainWindow::victory(){
    bool result = false;
    int flag = 0;
            if(spt % 2 != 0){
                kic++;
            }
            if(kic <= 5){
                if(opp - adm > admtime){
                    result = false;
                    flag++;
                }
                if(adm - opp > opptime){
                    result = true;
                    flag++;
                }
            }

            if(kic >= 6 && spt % 2 != 0){
                if(adm > opp){
                    result = true;
                    flag++;
                }
                if(adm < opp){
                    result = false;
                    flag++;
                }
            }
        if(flag != 0){
        if (result) {
                QMessageBox::information(this, "Victory", "You Win!");
            } else {
                QMessageBox::information(this, "Defeat", "You Lose!");
            }
        }
        return result;
    }


//// 在游戏主循环中检查游戏是否结束
//void MainWindow::gameLoop() {
//    // 游戏主循环逻辑
//    while (true) {
//        // 执行游戏逻辑，比如处理按键事件等

//        // 检查游戏是否结束
//        if (victory()) {
//            // 游戏结束，弹出消息框显示胜利消息
//            QMessageBox::information(this, "Game Over", "Congratulations! You Win!");
//            break; // 结束游戏循环
//        } else {
//            // 游戏结束，弹出消息框显示失败消息
//            QMessageBox::information(this, "Game Over", "Game Over! You Lose!");
//            break; // 结束游戏循环
//        }
//    }
//}

//void MainWindow::resizeEvent(QResizeEvent *event) {
//    QMainWindow::resizeEvent(event);
//    // 在窗口大小变化时更新红色区域的位置
//    updateRedAreaPosition();
//}
//bool MainWindow::eventFilter(QObject *obj, QEvent *event)
//{
//    if (obj == this && event->type() == QEvent::KeyPress)
//    {
//        QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
//        if (keyEvent->key() == Qt::Key_A) // 检测按下的键是否为A键
//        {
//            // 执行与A键对应的操作
//            qDebug() << "A key pressed!";
//            return true; // 表示事件已被处理
//        }
//    }
//    return QObject::eventFilter(obj, event);
//}
